# -*- coding: utf-8 -*-
import sys

from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class WidgetLINE(QLabel):
    LINE_ENABLE = 1
    LINE_DISABLE = 2
    LINE_WARNNING = 3

    def __init__(self, *args, **kwargs):
        text = kwargs.pop("text", "")
        super(WidgetLINE, self).__init__(*args, **kwargs)
        self.InitUI()

    def InitUI(self):
        # 背景颜色设置
        self.bgColor = QColor(0, 0, 0, 0)
        self.enableColor = QColor(0, 255, 0)
        self.disableColor = QColor(166, 166, 166)
        self.warnColor = QColor(255, 0, 0)
        self.currentColor = self.disableColor
        self.LineWidth = 2
        self.LinePatten = Qt.SolidLine
        self.turning_x = 0.5  # 转折点

        self.start_y = 0.5
        self.end_y_list = [0.1, 0.4, 0.6, 0.8]

    def setStartPos(self, pos_y):
        """
        设置左侧标记线位置
        :param pos_y: 最上到最下方 0 ~ 1
        :return:
        """
        self.start_y = pos_y
        self.update()

    # 设置取值接口
    def setStatus(self, status):
        """
            设置线条颜色

        :param status: 1-使能（绿色） 2-失效（灰色） 3-告警（红色）
        :return:
        """
        if status == self.LINE_ENABLE:
            self.currentColor = self.enableColor
        elif status == self.LINE_WARNNING:
            self.currentColor = self.warnColor
        else:
            self.currentColor = self.disableColor
        self.update()

    def paintEvent(self, event):
        width = self.width()
        height = self.height()

        painter = QPainter(self)  # 初始化painter
        # painter.translate(width / 2, height / 2)  # 坐标轴变换，调用translate()将坐标原点平移至窗口中心

        # 坐标刻度自适应
        # side = min(width, height)
        # painter.scale(side / 200.0, side / 200.0)
        self.bgWidth = width
        self.bgHeight = height

        # 启用反锯齿，使画出的曲线更平滑
        painter.setRenderHints(QPainter.Antialiasing | QPainter.TextAntialiasing)
        painter.begin(self)
        # 开始画图
        temppen = QPen(self.currentColor, self.LineWidth, self.LinePatten)
        painter.setPen(temppen)

        mid_x = width * self.turning_x
        # 起点连接线绘制
        posy = self.start_y * height
        painter.drawLine(0, posy, mid_x, posy)

        for h in self.end_y_list:
            endy = h * height
            painter.drawLine(mid_x, posy, mid_x, endy)
            painter.drawLine(mid_x, endy, width - self.LineWidth, endy)

        painter.end()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = WidgetLINE()
    # demo.InitUI()
    demo.show()
    demo.setGeometry(100, 100, 200, 200)
    demo.setStatus(demo.LINE_DISABLE)
    # demo.setValue(80)  # 设置指针指向
    # demo.setTitle("Title_____")  # 设置图标题
    sys.exit(app.exec_())
